package com.tengju.bff.interfaces.app.user.assembler;

import com.tengju.bff.interfaces.manage.star.vo.*;
import com.tengju.bff.interfaces.shared.MobileVO;
import com.tengju.data.domain.model.user.StarIndicator;
import com.tengju.user.application.archives.dto.StarLimitInfoDTO;
import com.tengju.user.application.archives.dto.SurveyContentDTO;
import com.tengju.user.application.archives.dto.SurveyQuestionResultDTO;
import com.tengju.user.domain.model.sign.*;
import com.tengju.user.domain.model.star.StarIndicatorType;
import com.tengju.user.domain.model.survey.*;
import com.tengju.user.domain.model.user.StarRoleEnum;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserStarInfo;
import com.tengju.user.domain.shared.DateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class StarSignInfoAssembler {

    static Long surveyId;
    static Long serviceProviderExamId;
    static Long operatorExamId;
    public static Long getSurveyId() {
        return surveyId;
    }

    @Value("${star.sign.survey.id}")
    public void setSurveyId(Long surveyId) {
        StarSignInfoAssembler.surveyId = surveyId;
    }

    public static Long getServiceProviderExamId() {
        return serviceProviderExamId;
    }

    @Value("${star.service.provider.exam.id}")
    public void setServiceProviderExamId(Long serviceProviderExamId) {
        StarSignInfoAssembler.serviceProviderExamId = serviceProviderExamId;
    }

    public static Long getOperatorExamId() {
        return operatorExamId;
    }

    @Value("${star.operator.exam.id}")
    public void setOperatorExamId(Long operatorExamId) {
        StarSignInfoAssembler.operatorExamId = operatorExamId;
    }

    public static StarSignInfoVO signInfoToVO(StarSignInfo starSignInfo) {
        if (starSignInfo == null) {
            return null;
        }
        StarSignInfoVO starSignInfoVO = new StarSignInfoVO();
        starSignInfoVO.setIdCode(starSignInfo.getIdCode().getValue());
        starSignInfoVO.setStarRole(starSignInfo.getStarRole());
        starSignInfoVO.setStarLevel(starSignInfo.getStarLevel());
        starSignInfoVO.setAccountType(starSignInfo.getAccountType());
        starSignInfoVO.setSignStep(starSignInfo.getStep());
        starSignInfoVO.setAuditDesc(starSignInfo.getAuditDesc());
        starSignInfoVO.setAuditStat(starSignInfo.getAuditStat());
        starSignInfoVO.setExpireDate(starSignInfo.getExpireDate());
        starSignInfoVO.setBeforeStarLevel(starSignInfo.getBeforeStarLevel());
        starSignInfoVO.setBeforeStarRole(starSignInfo.getBeforeStarRole());
        starSignInfoVO.setSurveyId(getSurveyId());
        if (StarRoleEnum.isOperator(starSignInfo.getStarRole())) {
            starSignInfoVO.setStarExamId(getOperatorExamId());
        } else {
            starSignInfoVO.setStarExamId(getServiceProviderExamId());
        }
        return starSignInfoVO;
    }

    public static SurveyConfigVO surveyConfigToVO(SurveyConfig surveyConfig) {
        if (surveyConfig == null) {
            return null;
        }
        SurveyConfigVO surveyConfigVO = new SurveyConfigVO();
        surveyConfigVO.setSurveyId(surveyConfig.getSurveyId().getValue());
        surveyConfigVO.setSurveyTitle(surveyConfig.getSurveyTitle());
        surveyConfigVO.setSurveyDesc(surveyConfig.getSurveyDesc());
        List<SurveyQuestionConfig> questionConfigList = surveyConfig.getQuestionConfigList();
        List<SurveyQuestionConfigVO> questionConfigVOList = new ArrayList<>();
        for (int i = 0; i < questionConfigList.size(); i++) {
            SurveyQuestionConfig v = questionConfigList.get(i);
            SurveyQuestionConfigVO questionConfigVO = new SurveyQuestionConfigVO();
            questionConfigVO.setCode(v.getCode());
            questionConfigVO.setOrder(i + 1);
            questionConfigVO.setRequired(v.getRequired().equals(SurveyQuestionRequireEnum.REQUIRED));
            questionConfigVO.setTitle(v.getTitle());
            questionConfigVO.setType(v.getType().getCode());
            List<SurveyQuestionConfigContent> configContentList = v.getConfigContentList();
            if (!CollectionUtils.isEmpty(configContentList)) {
                //临时兼容
                List<String> contentStrList = configContentList.stream().map(SurveyQuestionConfigContent::getTitle).collect(Collectors.toList());
                questionConfigVO.setParams(contentStrList);
                //长久适用
                List<SurveyQuestionConfigContentVO> questionConfigContentVOS = configContentList.stream()
                        .map(configContent -> SurveyQuestionConfigContentVO.builder()
                                .code(configContent.getCode())
                                .title(configContent.getTitle())
                                .build())
                        .collect(Collectors.toList());
                questionConfigVO.setConfigContent(questionConfigContentVOS);
            }
            questionConfigVOList.add(questionConfigVO);
        }
        surveyConfigVO.setQuestionConfigList(questionConfigVOList);
        return surveyConfigVO;
    }

    public static BlackNameListDetailVO starLimitToBlackDetailVO(StarLimitInfo starLimitInfo) {
        BlackNameListDetailVO blackNameListDetailVO = new BlackNameListDetailVO();
        blackNameListDetailVO.setIdCode(starLimitInfo.getIdCode().getValue());
        blackNameListDetailVO.setLimitHighestStarLevel(starLimitInfo.getLimitHighestStarLevel());
        blackNameListDetailVO.setRemark(starLimitInfo.getRemark());
        blackNameListDetailVO.setCreateTime(starLimitInfo.getCreateTime());
        return blackNameListDetailVO;
    }

    public static SurveyContent surveyContentDTOToEntity(SurveyContentDTO surveyContentDTO) {
        if (surveyContentDTO == null) {
            return null;
        }
        List<SurveyQuestionResultDTO> questionResultDTOList = surveyContentDTO.getQuestionResults();
        List<SurveyQuestionResult> surveyQuestionResultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(questionResultDTOList)) {
            questionResultDTOList.forEach(v -> {
                SurveyQuestionResult surveyQuestionResult = new SurveyQuestionResult(
                        v.getQuestionOrder(),
                        v.getQuestionCode(),
                        v.getQuestionTitle(),
                        v.getQuestionContent(),
                        null
                );
                surveyQuestionResultList.add(surveyQuestionResult);
            });
        }
        return new SurveyContent(
                UserIdCode.of(surveyContentDTO.getIdCode()),
                new SurveyId(surveyContentDTO.getSurveyId()),
                surveyQuestionResultList
        );
    }

    public static StarAuditRecordVO entityToAuditRecordVO(StarSignInfo starSignInfo) {
        StarAuditRecordVO starAuditRecordVO = new StarAuditRecordVO();
        starAuditRecordVO.setId(starSignInfo.getId().getValue());
        starAuditRecordVO.setUserId(starSignInfo.getIdCode().getValue());
        starAuditRecordVO.setIdCode(starSignInfo.getIdCode().getValue());
        starAuditRecordVO.setOriginStarLevel(starSignInfo.getBeforeStarLevel());
        starAuditRecordVO.setOriginStarRole(starSignInfo.getBeforeStarRole());
        starAuditRecordVO.setTargetStarLevel(starSignInfo.getStarLevel());
        starAuditRecordVO.setTargetStarRole(starSignInfo.getStarRole());
        starAuditRecordVO.setAuditStatus(starSignInfo.getAuditStat());
        starAuditRecordVO.setHandleType(starSignInfo.getTriggerType().byteValue());
        starAuditRecordVO.setAuditDesc(starSignInfo.getAuditDesc());
        starAuditRecordVO.setSignStatus(Optional.ofNullable(StarSignStatus.fromStepAndAuditStat(StarSignStepEnum.getByStep(Optional.ofNullable(starSignInfo.getStep()).orElse(null)),
                StarAuditStatEnum.fromVal(Optional.ofNullable(starSignInfo.getAuditStat()).orElse(null)))).map(StarSignStatus::getVal).orElse(null));
        starAuditRecordVO.setAuditBy(starSignInfo.getAuditBy());
        starAuditRecordVO.setAuditTime(DateUtil.localDateTimeToDate(starSignInfo.getAuditTime()));
        starAuditRecordVO.setSignStep(starSignInfo.getStep());
        StarSignStepEnum stepEnum = StarSignStepEnum.getByStep(starSignInfo.getStep());
        if (stepEnum != null) {
            starAuditRecordVO.setSignStepDesc(stepEnum.getDesc());
        }
        return starAuditRecordVO;
    }

    public static BlackUserDetailVO entityToBlackUserDetailVO(UserStarInfo userStarInfo) {
        BlackUserDetailVO blackUserDetailVO = new BlackUserDetailVO();
        long idCode = userStarInfo.getIdCode().getValue();
        StarContractInfo starContractInfo = userStarInfo.getStarContractInfo();
        LocalDateTime starLevelLastChangeTime = userStarInfo.getStarLevelLastChangeTime();
        UserInfo userInfo = userStarInfo.getUserInfo();
        StarSignInfo starSignInfo = userStarInfo.getStarSignInfo();
        StarLimitInfo starLimitInfo = userStarInfo.getStarLimitInfo();
//        blackUserDetailVO.setUserId(userStarInfo.getUserInfo().getId());
        blackUserDetailVO.setIdCode(idCode);
        blackUserDetailVO.setMobile(new MobileVO(userInfo.getWholePhoneNumber()));
        blackUserDetailVO.setNickName(userInfo.getNickName());
        blackUserDetailVO.setStarRole(userInfo.getStarRole());
        blackUserDetailVO.setStarLevel(userInfo.getStarLevel());
        if (starContractInfo != null) {
            blackUserDetailVO.setAgreementStatus(starContractInfo.getContractStatus().byteValue());
            blackUserDetailVO.setAgreementExpireDate(starContractInfo.getContractExpireDate());
        }
        if (starSignInfo != null) {
            blackUserDetailVO.setInviteId(starSignInfo.getId().getValue());
            blackUserDetailVO.setHasInviteFlag(true);
            blackUserDetailVO.setAuditDesc(starSignInfo.getAuditDesc());
        }
        blackUserDetailVO.setUpdateTime(starLevelLastChangeTime);
        blackUserDetailVO.setBlackListFlag(starLimitInfo != null);
        if (starLimitInfo != null) {
            blackUserDetailVO.setLimitHighestStarlevel(starLimitInfo.getLimitHighestStarLevel());
        }
        return blackUserDetailVO;
    }

    public static UserStarLevelDetailVO userStarToStarLevelDetailVO(UserStarInfo userStarInfo) {
        UserStarLevelDetailVO userStarLevelDetailVO = new UserStarLevelDetailVO();
        StarContractInfo starContractInfo = userStarInfo.getStarContractInfo();
        LocalDateTime starLevelLastChangeTime = userStarInfo.getStarLevelLastChangeTime();
        UserInfo userInfo = userStarInfo.getUserInfo();
        StarSignInfo starSignInfo = userStarInfo.getStarSignInfo();
        long idCode = userStarInfo.getIdCode().getValue();
//        userStarLevelDetailVO.setUserId(userStarInfo.getUserInfo().getId());
        userStarLevelDetailVO.setIdCode(idCode);
        userStarLevelDetailVO.setMobile(new MobileVO(userInfo.getWholePhoneNumber()));
        userStarLevelDetailVO.setNickName(userInfo.getNickName());
        userStarLevelDetailVO.setName(userInfo.getName());
        userStarLevelDetailVO.setStarRole(userInfo.getStarRole());
        userStarLevelDetailVO.setStarLevel(userInfo.getStarLevel());
        if (starContractInfo != null) {
            userStarLevelDetailVO.setAgreementStatus(starContractInfo.getContractStatus().byteValue());
            userStarLevelDetailVO.setAgreementExpireDate(starContractInfo.getContractExpireDate());
        }
        if (starSignInfo != null) {
            userStarLevelDetailVO.setHasInviteFlag(true);
            userStarLevelDetailVO.setAuditStatus(starSignInfo.getAuditStat());
            userStarLevelDetailVO.setAuditDesc(starSignInfo.getAuditDesc());
            userStarLevelDetailVO.setNowStatus(starSignInfo.getStep());
            userStarLevelDetailVO.setHandleType(starSignInfo.getTriggerType().byteValue());
            userStarLevelDetailVO.setOriginStarRole(starSignInfo.getBeforeStarRole());
            userStarLevelDetailVO.setOriginStarLevel(starSignInfo.getBeforeStarLevel());
            userStarLevelDetailVO.setTargetStarRole(starSignInfo.getStarRole());
            userStarLevelDetailVO.setTargetStarLevel(starSignInfo.getStarLevel());
        }
        userStarLevelDetailVO.setStarUpdateTime(starLevelLastChangeTime);
        return userStarLevelDetailVO;
    }

    public static StarIndicatorDetailVO starIndicatorStatToVO(StarIndicator starIndicatorStat) {
        StarIndicatorDetailVO starIndicatorDetailVO = new StarIndicatorDetailVO();
        starIndicatorDetailVO.setIndicatorCode(Optional.ofNullable(starIndicatorStat).map(StarIndicator::getStarIndicatorType).map(StarIndicatorType::getCode).orElse(""));
        starIndicatorDetailVO.setIndicatorName(Optional.ofNullable(starIndicatorStat).map(StarIndicator::getStarIndicatorType).map(StarIndicatorType::getDesc).orElse(""));
        starIndicatorDetailVO.setIndicatorValue(Optional.ofNullable(starIndicatorStat).map(StarIndicator::getIndicatorValue).orElse(null));
        return starIndicatorDetailVO;
    }

    public static StarConfIndicatorVO starConfIndicatorToVO(StarConfIndicator starConfIndicator) {
        StarConfIndicatorVO starConfIndicatorVO = new StarConfIndicatorVO();
        starConfIndicatorVO.setConfName(starConfIndicator.getConfName());
        starConfIndicatorVO.setConfValue(starConfIndicator.getConfValue());
        return starConfIndicatorVO;
    }

    public static StarLimitInfo limitDTOToEntity(StarLimitInfoDTO starLimitInfoDTO) {
        return new StarLimitInfo(
                UserIdCode.of(starLimitInfoDTO.getIdCode()),
                starLimitInfoDTO.getLimitHighestStarLevel(),
                starLimitInfoDTO.getRemark(),
                starLimitInfoDTO.getStatus(),
                starLimitInfoDTO.getCreateTime()
        );
    }
}
